package com.cz.ssyx.acl.controller;

import com.cz.ssyx.acl.service.PermissionService;
import com.cz.ssyx.acl.service.RoleService;
import com.cz.ssyx.common.result.Result;
import com.cz.ssyx.model.acl.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @descriptions:
 * @author: Joker.
 * @date: 2024/4/16 18:27
 * @version: 1.0
 */

@Api(tags = "菜单管理接口")
//@CrossOrigin
@RestController
@RequestMapping("/admin/acl/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;


    //获取权限(菜单/功能)列表
    @ApiOperation("获取权限(菜单/功能)列表")
    @GetMapping
    public Result getPermissionList() {
        List<Permission> permissions = permissionService.queryAllPermission();
        return Result.ok(permissions);
    }

    @ApiOperation("新增菜单")
    @PostMapping("/save")
    public Result save(@RequestBody Permission permission) {
        permissionService.save(permission);
        return Result.ok(null);
    }

    @ApiOperation("修改菜单")
    @PutMapping("/update")
    public Result updateById(@RequestBody Permission permission) {
        permissionService.updateById(permission);
        return Result.ok(null);
    }

    @ApiOperation("递归删除菜单")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        permissionService.removeChildById(id);
        return Result.ok(null);
    }

    @ApiOperation("获取角色的权限信息")
    @GetMapping("/toAssign/{roleId}")
    public Result toAssign(@PathVariable Long roleId){

        Map<String, Object> map = permissionService.getRolePermission(roleId);

        return Result.ok(map);
    }

    @ApiOperation("为角色分配权限")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestParam Long roleId, @RequestParam List<Long> permissionId) {
        permissionService.doRolePermission(roleId, permissionId);
        return Result.ok(null);
    }

}
